<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    destroyOnClose
    :title="title"
    :showCancelBtn="true"
    :showOkBtn="true"
    :canFullscreen="false"
    :centered="true"
    :width="480"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../Account.data';
  import { saveOrUpdate, accountById, coopSelect } from '../Account.api';
  import { queryDepartTreeSync, getUsersByDepartId } from '/@/api/common/api';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema, clearValidate }] = useForm({
    labelWidth: 120,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    const coopOptions = ref([]);
    // 1、部门下接
    const departOptions = ref([]);
    if (unref(isUpdate)) {
      const account = await accountById({ id: data.record.id });

      data.record = Object.assign(data.record, account);
      if (data.record.sflx == 1) {
        data.record.je = data.record.skje;
      } else if (data.record.sflx == 2) {
        data.record.je = data.record.fkje;
      }
      const coopPath = data.record.coopPath && data.record.coopPath.split(',');
      const arrays = coopPath <= 0 ? "" : coopPath.filter((item) => item); //[366,381,382]//e
      //console.info(coopPath, arrays)
      data.record.cId = arrays;
    }

    // 1、部门下接数据 queryDepartTreeSync({ pid: '98608507967900000' });
    departOptions.value = await queryDepartTreeSync({ pid: '1798608507967900000' });
    coopOptions.value = await coopSelect();
    updateSchema([
     {
        field: 'region',
        componentProps: {
          options: departOptions,
        },
      },
      {
        field: "cId",
        componentProps: { options: coopOptions }
      },
    ])

    // 无论新增还是编辑，都可以设置表单值
    //console.info("data.record@@", data.record.cId)
    if (typeof data.record === 'object') {
      setFieldsValue({
        ...data.record,
      });
    }

    await clearValidate();
  });

  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      if (values.sflx == 1) {
        //收入
        values.skje = values.je
        values.fkje = 0
        values.fklx = ''
        //values.coopId = null
      } else if (values.sflx == 2) {
        values.fkje = values.je
        values.skje = 0
        //values.fklx = fklx // 不记得用来做什么的了
        //values.bcId = null
      }

      //提交表单
      await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', { isUpdate: isUpdate.value, values });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped></style>
